16
https://raw.githubusercontent.com/wmm1996528/unidbg_douyin10/master/src/test/java/com/github/unidbg/android/ida/ProtocolTest.java
package com.github.unidbg.android.ida;

import com.github.unidbg.debugger.ida.Utils;
import com.github.unidbg.utils.Inspector;
import junit.framework.TestCase;
import org.apache.commons.codec.binary.Hex;

import java.nio.ByteBuffer;

public class ProtocolTest extends TestCase {

    public void testSymbols() throws Exception {
        testSymbol("00000518324bff4001699600000070735f6765747069640083c500010370676c6f62616c5f6c6f6f6b757000c008c6e80000005f5f46494e495f41525241595f5f0031000102494e49545f41525241595f5f0009000102505245494e49545f41525241595f5f00c008dee9000101737461727400c0069f22000001556e77696e645f526573756d6500c00091190001015a5374397465726d696e6174657600898d000102646c507600c000a8ed0000015f61656162695f75696469760090150001096e77696e645f6370705f70723100c00094610001026378615f626567696e5f6361746368008e85000006707572655f7669727475616c0088710001026778785f706572736f6e616c6974795f763000c000fac1000100666f70656e525400c00059910001017265655f64656275675f6576656e7400c00076c100000071616c6c6f635f6f725f7468726f7700869d00010166636c6f7365008d75000102696e64636c6f736500812900010566697273740080910001056e6578740088b90001026f70656e00969d00000272656164008747000004650080f70001016765746c696e6500aead000001736e7072696e746600808d0000027363616e6600ad3d000101766563746f725f7265736572766500af09000000737472327573657200c000a4f90000005f5a4e537431316c6f6769635f6572726f72433245504b6300a091000006326c656e6774685f6572726f724431457600c002f6ec0000025449537431316c6f6769635f6572726f72004d000007326c656e6774685f6572726f720080d500010639657863657074696f6e00c000f02c00010353537431316c6f6769635f6572726f720040000007326c656e6774685f6572726f720080db00010639657863657074696f6e00c000ef93000003564e31305f5f637878616269763131375f5f636c6173735f747970655f696e666f45002900001132305f5f73695f636c6173735f747970655f696e666f450081cd000004537431326c656e6774685f6572726f7200c00319940001026e776a00c000a9fd0000015f61656162695f6964697600809d00000c6d6f640080b100010875696469766d6f640090cd0001096e77696e645f6370705f70723000c00095c50001026378615f616c6c6f636174655f657863657074696f6e002b000006667265655f657863657074696f6e00330000067468726f7700c0015e9100010063616c635f66696c655f637263333200c00492b40000036c756900c0042efc0001016c6f73655f6c696e70757400c00051910001016f70795f64656275675f6576656e7400c004aea0000000646562756700c00435cc0001026c5f7161746578697400c000ee910001006765745f656c665f64656275675f66696c655f6469726563746f727900c000ee45000000696e7465727200a8e10001006f70656e5f6c696e70757400a819000000716174657869740093a9000101626173656e616d6500710001016469726e616d6500885100000166696c65657869737400acb1000001676574656e7600b3310001016973616273706174680081b50000016d616b655f66756c6c5f706174680084d100010570617468008a710000017374617400891d00000372647570008b450000046e63707900814d000104746f6b009fc900000176736e7072696e7466008641000101776169745f74696d656400c00097910001007365745f64656275675f6576656e745f636f646500c000a7530000016b69705f73706163657300815300010174726973747200c00406d4000000756e6465725f646562756767657200");
        testSymbol("0000051d3251ff40068392000000766572726f7200c00093cd00010173686f775f68657800c000940d00000077696e65727200c00178750000005f5f61656162695f6c6c73720087750000096d756c004e000108756c6469766d6f6400c000a9040001026378615f67756172645f61637175697265007500000c72656c6561736500c000fded0001006c726561646279746573008209000100716c7265616400470000027365656b0025000103697a6500af6f0000017374706e63707900aae9000100737761703634008715000000756e7061636b5f786c656231323800c0004aad00000063616c6c5f73797374656d00c00383210001016f6d70696c655f6964635f66696c6500839100000c7465787400c0007ac900000064656c5f696463765f6174747200c000679d0001006576616c5f657870720084410000056964635f736e697070657400924d00000066696e645f6964635f636c61737300b3050000027273745f696463765f6174747200c002ecc500000172656164627974657300c002e91f000103655f6964637600c002e98b0000017772697465627974657300c002e24d0001006765745f696463765f6174747200c002ebb90000046e7365635f7374616d7000c002e801000100696463765f6f626a656374008b7d0000006c6173745f696463765f6174747200c002df890000006d656d72657600c002f9790001006e6578745f696463765f61747472009a35000000707265765f696463765f6174747200c002d3bf0000007166676574630029000102707574630080e70000057300851f0001027365656b00c0006033000103697a6500c0005fff00000274656c6c0083670001016d6b64697200b05f000002757465785f6c6f636b000f000007756e6c6f636b00ac2900010172656e616d6500ac39000001736574656e7600a0dd0001026c656570008b89000101756e6c696e6b00c00082a5000100726f746174655f6c656674009e8d0000007365745f6572726f725f737472696e6700c0027709000104696463765f6174747200c0027695000004716572726e6f00c00340410001007468726f775f6964635f657863657074696f6e00c00087fd0000006164645f6964635f636c61737300a609000000636f70795f6964637600c0026aa10000006765745f61666c61677300a7c9000004716572726e6f00c001a0d10001006c65785f6765745f66696c655f6c696e6500ad27000008746f6b656e00ae4b000104696e69745f66696c6500bacb000109737472696e6700c0006b870000047072696e745f746f6b656e00afa10001047465726d5f66696c6500c0020aa100000071616c6c6f63008e950000017374726572726f7200c001eb0d0001007365745f6865616465725f7061746800c000fac10001017761705f696463767300c0026c2d000000757466385f757466313600c001e9690000005f5f61656162695f6c6c736c00c0046785000100696463765f666c6f61740081ad000105696e7436340080f10001056c6f6e67008509000105737472696e6700c002ac9d0000007265616c746f61736300c002700500010063616c6c5f6964635f66756e6300c00084650001006576616c5f657870725f6c6f6e67005d0001056964635f657870720080e90000017865635f73797374656d5f73637269707400838100010066696e645f6578746c616e670081250001016f725f616c6c5f6578746c616e67730080910001006765745f63757272656e745f6578746c616e6700c00158cd0000046964635f66696c656e616d6500c00152e90000046e616d655f76616c756500");
        testSymbol("0000051e3250ff4003433a000000696e7374616c6c5f6578746c616e670080c100000072656d6f76655f6578746c616e670081ed00010073656c6563745f6578746c616e6700c00410150000005f5a64615076002b0001026e616a00c003ed370001006164645f6964635f66756e63008ad100010867766172009a73000000636f64655f686967686c696768745f626c6f636b00ad670001026d70696c655f6964635f736e697070657400c000f04f00000172656174655f6c6578657200c000d02700010064656c5f6964635f66756e6300c000d0590000027374726f795f6c6578657200c000cfd500010066696e645f6964635f66756e63008e290001096776617200c000503d0000006765745f696463765f636c6173735f6e616d6500c006ea9400000069735f6964615f6b65726e656c00c00430840001007061636b5f6464009aaf0000007163616c6c6f63008fe70000016d757465785f63726561746500967b0000076672656500c00341830001007365745f6964635f64746f72008261000108676574617474720081510001086d6574686f640082810000087365746174747200c0030339000000756e7061636b5f646400c00405340000005f5a54495374396261645f616c6c6f6300c000f0a0000103535374396261645f616c6c6f6300c0017d990001015f61656162695f6c6173720086cc0000096469766d6f6400c000ac2c0001026378615f656e645f636174636800c0012521000100617363746f7265616c00c002aef10001006372656174655f696463765f72656600a449000000646565705f636f70795f6964637600a6650001027265665f6964637600c0029fd1000000656164640082bd000001636d700084d100000164697600858100000165746f6c0081c9000005363400790000016c746f6500809d0000053634008bad0001016d756c00c0028f010001006765745f696463765f736c696365008199000100696463765f6e756d00c002f8b5000001735f6d61696e5f74687265616400c002fccd0001006d6f76655f69646376008c810001007072696e745f6964637600c003294100000071737472327573657200c00098850001007365745f6572726f725f6461746100c0027599000104696463765f736c69636500c003aca10000005f5a4e5374365f5f6e646b3131325f5f6e6578745f7072696d65456a00c000b7640000015f61656162695f663275697a0051000109636d706c7400820d0001096469760081990001096d756c0080b50001087569326600c0014a100001006261636b5f6368617200c00067450001006765745f6964615f7375626469727300811100010373797366696c6500c00209a10001006c65785f646566696e655f6d6163726f008839000104656e756d5f6d6163726f7300c000677d0000046765745f746f6b656e3200c00069e10001047365745f6f7074696f6e730091ed000004756e646566696e655f6d6163726f00c0023c99000000716374696d6500c0006fe1000101657272636f646500c00059b500000167657463776400853d000001697364697200c001bd0100010062617369635f7061727365725f706172736500c001cdbd0000007174696d65363400a7550001006372656174655f6279746561727261795f6c696e70757400c000655d0000077a69705f6c696e70757400c000624d000100666f70656e524200809100000557420082d50000007061636b5f647100650001067700c0004e550001007165727273747200c000616d0000016677726974650086970000017265616c6c6f6300adb3000001767072696e746600");
        testSymbol("0000051c3252ff4006a5e2000000737761705f76616c756500bfed000100756e7061636b5f64710080e50001087700c000784d000100766c6f616465725f6661696c75726500a89d0000006765745f6572726f725f64617461003500000a737472696e6700c00084a90000006c61756e63685f70726f63657373008db100000072656c6f636174655f72656c6f626a00c000a105000100636c696f7074735f745f6170706c7900c0007c69000000716578697400c0008e15000100626974636f756e74008e8d000000636c696f7074735f745f61646400c0007c4100000071686f737432616464725f00a3e100000173656d5f6372656174650080ab00000566726565008bd7000105706f73740085fd000105776169740082a90000017468726561645f637265617465008e650000086672656500884500000176657072696e746600a49d0001017374726674696d65363400a40100000176667072696e746600c000c8110000005f5a4e537439657863657074696f6e4432457600c00186d10001006164645f756e646f5f7265636f726400c004a79800000064626700c004a7aa00010066756e635f6861735f73746b6672616d655f686f6c65000f0001006765745f6964705f6e616d65001d000004756e646f5f706172616d000f00000069735f7265706c6179696e675f756e646f004b0000006c72656164004f0001006d7573745f6a6f75726e616c00330001006e62697473003100000072656769737465725f756e646f5f68616e646c65727300130001007365745f6964615f73746174650086270000007673686f775f6865785f66696c650082c1000000657874656e645f7369676e0082590001006c6f67326365696c008113000004666c6f6f72008255000000726f756e645f646f776e5f706f77657232001b00010675705f706f77657232003500000063616c635f6372633332008d950000016c696f7074735f745f66696e645f6c6f6e67003500010f73686f727400808100000a75736167650091e90000006765745f65727264657363009f0900010070617273655f636f6d6d616e645f6c696e650089590000066462676f70747300c000b15d00000071636c65616e6c696e6500c000b30d000101756f74655f636d646c696e655f617267008d010000006765745f757466385f63686172004900010069735f76616c69645f7574663800816d000000707265765f757466385f636861720080b500000175745f757466385f63686172008231000100717374726c656e00831d000001757374726c656e00490000056e6370790080a9000100736b69705f7574663800818900000075746631365f7574663800c001fff50000005f5f656d75746c735f6765745f6164647265737300c001f9a10001007671706572726f720099550000006565746f6c363475008b7d0001016c64657870008d11000002746f653634750095b50001016d646e6f726d00350001026f7669002d0001046f0082cd000101736869667400ae790000006765745f667076616c75655f6b696e64008bad0000047370656369616c5f666f6c646572008215000104757365725f6964616469720086490001006964616469720081290001007365617263685f7061746800a27100000076716d616b65706174680091950001006372656174655f67656e657269635f6c696e707574003b0000006d616b655f6c696e70757400a2a7000000716667657473008685000102696c656c656e677468009aa30001016c66696c65008107000002676574630080f5000105730081990000057a0096750000017265616400");
        testSymbol("0000034a322fff40173d610000005f5a54495374313172616e67655f6572726f7200808d00010732646f6d61696e5f6572726f7200610000086f75745f6f665f72616e676500210000073372756e74696d655f6572726f72002d000007346f766572666c6f775f6572726f72002100000735756e646572666c6f775f6572726f720080ad00010736696e76616c69645f617267756d656e7400b0e400010353537431316c6f6769635f6572726f72007100000872616e67655f6572726f7200808200010732646f6d61696e5f6572726f7200510000086c656e6774685f6572726f7200210000086f75745f6f665f72616e676500310000073372756e74696d655f6572726f720021000007346f766572666c6f775f6572726f72002100000735756e646572666c6f775f6572726f720080b100010736696e76616c69645f617267756d656e7400b111000003565374313172616e67655f6572726f7200808d00010732646f6d61696e5f6572726f72006d0000086f75745f6f665f72616e6765004d000007346f766572666c6f775f6572726f72002100000735756e646572666c6f775f6572726f720080ad00010736696e76616c69645f617267756d656e7400c000f6dc0001024e4b537431306261645f747970656964347768617445760031000106386261645f6361737434776861744576000d000003537431306261645f747970656964433145760001000012324576001500001144304576000500011231457600010000123245760041000105386261645f6361737443314576000100000f324576001500000e44304576000500010f314576000100000f324576001700010539747970655f696e666f44304576000300011031457600c000f8500000025449537431306261645f747970656964000d000106386261645f6361737400b0e000010353537431306261645f747970656964000d000106386261645f63617374000e00010639747970655f696e666f00b0cd00000356537431306261645f7479706569640015000106386261645f63617374002900000639747970655f696e666f00c000f734000102537431337365745f7465726d696e61746550467676450031000105347365745f756e6578706563746564504676764500816d0000015f6378615f64656d616e676c6500");
    }

    private void testSymbol(String hex) throws Exception {
        byte[] data = Hex.decodeHex(hex.toCharArray());
        ByteBuffer buffer = ByteBuffer.wrap(data);
        int packetSize = buffer.getInt();
        buffer.get(); // type

        data = new byte[packetSize];
        buffer.get(data);
        buffer = ByteBuffer.wrap(data);

        long count = Utils.unpack_dd(buffer);
        for (int i = 0; i < count; i++) {
            long address = Utils.unpack_dq(buffer);
            long b2 = Utils.unpack_dd(buffer);
            long b3 = Utils.unpack_dd(buffer);
            String name = Utils.readCString(buffer);
            System.out.println("address=0x" + Long.toHexString(address) + ", b2=" + b2 + ", b3=" + b3 + ", name=" + name);
        }

        data = new byte[buffer.remaining()];
        buffer.get(data);
        Inspector.inspect(data, "Remaining");
    }

    public void testSingle() throws Exception {
        testSingle("0000003600028080c0008966c0008966ff4015400100012f73797374656d2f6c69622f6c6962737464632b2b2e736f00ff4015400100b001000001");
        testSingle("0000003500028080c0008966c0008966ff4014f00100012f73797374656d2f6c69622f6c69626c6f672e736f00ff4014f00100c0005001000001");
        testSingle("0000003700028080c0008966c0008966ff4014700100012f73797374656d2f6c69622f6c696278686f6f6b2e736f00ff4014700100c0008001000001");
        testSingle("0000003300028080c0008966c0008966ff4010b00100012f73797374656d2f6c69622f6c69626d2e736f00ff4010b00100c001b001000001");
        testSingle("0000003400028080c0008966c0008966ff4010700100012f73797374656d2f6c69622f6c6962646c2e736f00ff4010700100c0004001000001");
        testSingle("0000003300028080c0008966c0008966ff400ac00100012f73797374656d2f6c69622f6c6962632e736f00ff400ac00100c005b001000001");

        testSingle("00000041000201c0008966c0008966ff400cc11500012f73797374656d2f62696e2f616e64726f69645f7365727665725f372e3400ff4000000100c00a200100ff4000000100");
        testSingle("0000004200018400c0008966c0008966ff400cc11500012f73797374656d2f62696e2f616e64726f69645f7365727665725f372e3400ff4000000100c00a200100ff4000000100");
    }

    private void testSingle(String hex) throws Exception {
        byte[] data = Hex.decodeHex(hex.toCharArray());
        ByteBuffer buffer = ByteBuffer.wrap(data);
        int packetSize = buffer.getInt();
        buffer.get(); // type

        data = new byte[packetSize];
        buffer.get(data);
        buffer = ByteBuffer.wrap(data);

        byte type = buffer.get();
        long magic = Utils.unpack_dd(buffer);
        long pid = Utils.unpack_dd(buffer);
        long tid = Utils.unpack_dd(buffer);
        long pc = Utils.unpack_dd(buffer);
        short s1 = buffer.getShort();
        String path = Utils.readCString(buffer);
        long base = Utils.unpack_dd(buffer);
        byte b1 = buffer.get();
        long size = Utils.unpack_dd(buffer);
        byte b2 = buffer.get();
        long test = Utils.unpack_dd(buffer);
        boolean dylib = buffer.get() == 1;
        data = new byte[buffer.remaining()];
        buffer.get(data);
        Inspector.inspect(data, "type=" + type + ", magic=" + magic + ", pid=" + pid + ", tid=" + tid +
                ", pc=0x" + Long.toHexString(pc) + ", s1=" + s1 + ", path=" + path +
                ", base=0x" + Long.toHexString(base) + ", b1=" + b1 + ", size=0x" + Long.toHexString(size) + ", b2=" + b2 + ", test=0x" + Long.toHexString(test) +
                ", dylib=" + dylib);
    }

    public void testLoads() throws Exception {
        byte[] data = Hex.decodeHex("000002e00005150100ff4000000100a00100152f73797374656d2f6c69622f6c69627474456e63727970742e736f00000100ff4000300100a00100162f73797374656d2f6c69622f6c69627474456e63727970742e736f00000100ff4000500100c000500100152f73797374656d2f6c69622f6c6962737464632b2b2e736f00000100ff4000a00100a00100162f73797374656d2f6c69622f6c6962737464632b2b2e736f00000100ff4000c00100c007300100152f73797374656d2f6c69622f6c6962632e736f00000100ff4007f00100c001100100162f73797374656d2f6c69622f6c6962632e736f00000100ff4009000100a00100152f73797374656d2f6c69622f6c6962646c2e736f00000100ff4009200100a00100162f73797374656d2f6c69622f6c6962646c2e736f00000100ff4009400100c001f00100152f73797374656d2f6c69622f6c69626d2e736f00000100ff400b400100a00100162f73797374656d2f6c69622f6c69626d2e736f00000100ff4018000100c001b00100152f73797374656d2f6c69622f6c6962686f6f6b7a7a2e736f00000100ff4019c00100b00100162f73797374656d2f6c69622f6c6962686f6f6b7a7a2e736f00000100ff4019f00100c000800100152f73797374656d2f6c69622f6c69626c6f672e736f00000100ff401a700100a00100162f73797374656d2f6c69622f6c69626c6f672e736f00000100ff401ab00100c008800100152f73797374656d2f6c69622f6c6962632b2b2e736f00000100ff4023400100c000600100162f73797374656d2f6c69622f6c6962632b2b2e736f00000100ff4023a00100c000600100152f73797374656d2f6c69622f6c696278686f6f6b2e736f00000100ff4024000100a00100162f73797374656d2f6c69622f6c696278686f6f6b2e736f00000100ffbfe0000100c020000100165b737461636b5d00000100fffffe000100c001000100155b7376635d00000100ffffff000100c001000100155b6b65726e656c5d0000".toCharArray());
        ByteBuffer buffer = ByteBuffer.wrap(data);
        int packetSize = buffer.getInt();
        buffer.get(); // type

        data = new byte[packetSize];
        buffer.get(data);
        buffer = ByteBuffer.wrap(data);

        Utils.unpack_dd(buffer);
        long count = Utils.unpack_dd(buffer);
        for (int i = 0; i < count; i++) {
            long b0 = Utils.unpack_dq(buffer);
            long base = Utils.unpack_dd(buffer);
            byte b1 = buffer.get();
            long size = Utils.unpack_dd(buffer);
            int mask = buffer.getShort();
            String name = Utils.readCString(buffer);
            byte b2 = buffer.get();
            boolean d = ((mask >> 4) & 1) != 0;
            boolean r = ((mask >> 2) & 1) != 0;
            boolean w = ((mask >> 1) & 1) != 0;
            boolean x = ((mask) & 1) != 0;
            System.out.println("b0=0x" + Long.toHexString(b0) + ", base=0x" + Long.toHexString(base) + ", b1=0x" + Integer.toHexString(b1) +
                    ", d=" + d + ", r=" + r + ", w=" + w + ", x=" + x +
                    ", size=0x" + Long.toHexString(size) +
                    ", mask=0x" + Integer.toHexString(mask) + ", name=" + name + ", b2=" + b2);
        }
    }

    public void testPackDD() {
        assertEquals("c0008a37", Hex.encodeHexString(Utils.pack_dd(0x8a37)));
    }

}
